{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "advanced-ancient",
   "metadata": {},
   "source": [
    "# Australian Tax File Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "worldwide-serbia",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "stretch-rolling",
   "metadata": {},
   "source": [
    "The function `clean_au_tfn()` cleans a column containing Australian Tax File Numbers (TFN) strings, and standardizes them in a given format. The function `validate_au_tfn()` validates either a single TFN strings, a column of TFN strings or a DataFrame of TFN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regulated-confirmation",
   "metadata": {},
   "source": [
    "TFN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"123456782\"\n",
    "* `standard`: TFN strings with proper whitespace in the proper places, like \"123 456 782\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_au_tfn()` and `validate_au_tfn()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "angry-victoria",
   "metadata": {},
   "source": [
    "### An example dataset containing TFN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recreational-queens",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"tfn\": [\n",
    "            \"123 456 782\",\n",
    "            \"999 999 999\",\n",
    "            \"123456782\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "auburn-strain",
   "metadata": {},
   "source": [
    "## 1. Default `clean_au_tfn`\n",
    "\n",
    "By default, `clean_au_tfn` will clean tfn strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "buried-magnitude",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_au_tfn\n",
    "clean_au_tfn(df, column = \"tfn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "canadian-result",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "conscious-adventure",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "internal-telephone",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "advised-recycling",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_tfn(df, column = \"tfn\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "thick-dubai",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "about-concept",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_tfn(df, column = \"tfn\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tamil-share",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned TFN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "insured-harvard",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_tfn(df, column=\"tfn\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "first-manner",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "wanted-registration",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "challenging-chess",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_tfn(df, \"tfn\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "educational-tuner",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "figured-bidding",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_tfn(df, \"tfn\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sacred-definition",
   "metadata": {},
   "source": [
    "## 4. `validate_au_tfn()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "short-heater",
   "metadata": {},
   "source": [
    "`validate_au_tfn()` returns `True` when the input is a valid TFN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_au_tfn()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_au_tfn()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_au_tfn()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "embedded-consultation",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_au_tfn\n",
    "print(validate_au_tfn(\"123 456 782\"))\n",
    "print(validate_au_tfn(\"99 999 999\"))\n",
    "print(validate_au_tfn(\"123456782\"))\n",
    "print(validate_au_tfn(\"51 824 753 556\"))\n",
    "print(validate_au_tfn(\"hello\"))\n",
    "print(validate_au_tfn(np.nan))\n",
    "print(validate_au_tfn(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "corrected-macedonia",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "terminal-mainstream",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_tfn(df[\"tfn\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "protective-discrimination",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "constant-nightlife",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_tfn(df, column=\"tfn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "accepting-trance",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fourth-pendant",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_au_tfn(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "worthy-cycling",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
